package com.zrz.controller;

import com.zrz.model.auto.Admin;
import com.zrz.model.auto.Redcode;
import com.zrz.model.auto.User;
import com.zrz.resp.RecordResp;
import com.zrz.resp.VipCountResp;
import com.zrz.service.AdminService;
import com.zrz.service.UserService;
import com.zrz.vo.PositionVO;
import jodd.madvoc.meta.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;


    @RequestMapping("/toRegisterPage")
    public String toRegisterPage() {
        return "register";
        //重定向
    }

    @RequestMapping("/toLoginPage")
    public String toLoginPage() {
        return "login";
        //重定向
    }

    @RequestMapping("/register")
    public String register(Admin a, HttpServletRequest request) {
        Admin admin = adminService.add(a);
        return "business";
    }

    @RequestMapping("/addBusiness")
    public String addBusiness() {
        return "business";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map login(String userName, String password, HttpServletRequest request) {
        Map map=new HashMap();
        Admin admin = adminService.login(userName, password);
        if (admin == null) {
            map.put("code",-1);
            map.put("msg","用户名或密码错误");
            return map;
        }
        request.getSession().setAttribute("adminUserId", admin.getId());
        map.put("code",1);
        map.put("msg","登录成功");
        return map;
    }

    @ResponseBody
    @RequestMapping("/position")
    public List<PositionVO> getPosition(double latitude, double longitude) {
        return adminService.getMallInofo(latitude, longitude);
    }

    @ResponseBody
    @RequestMapping("/info")
    public Admin info(Integer id) {
        return adminService.getAdmin(id);
    }

    @RequestMapping("/list")
    public String toAdminList(Model model) {
        List<User> userList = adminService.getAllUser();
        model.addAttribute("userList", userList);
        return "list";
        //重定向
    }

    @RequestMapping("/receive")
    @ResponseBody
    public Map getRed(HttpServletRequest request, Integer uid) {
        Integer id = (Integer) request.getSession().getAttribute("adminUserId");
        Admin admin=adminService.getAdmin(id);
        return adminService.getRed(uid, admin);
    }

    @RequestMapping(value = "/pc/list")
    public String pcPages(Model model, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("pcUser");
        if (admin != null) {
            List<Admin> adminList = adminService.getAdminList();
            model.addAttribute("adminList", adminList);
            return "/pcPages/list";
        } else {
            return "redirect:/admin/pc/toLogin";
        }

    }

    @RequestMapping(value = "/pc/toLogin", method = RequestMethod.GET)
    public String pcLoginPage() {
        return "/pcPages/login";
    }

    @ResponseBody
    @RequestMapping(value = "/pc/login", method = RequestMethod.POST)
    public Map pcLogin(String username, String password, HttpServletRequest request) {
        Map map = new HashMap();
        if (username.equals("admin") && password.equals("yuemingpassw0rd")) {
            Admin admin = new Admin();
            admin.setUsername("yuemingpassw0rd");
            admin.setPassword("");
            request.getSession().setAttribute("pcUser", admin);
            map.put("code", 0);
        } else {
            map.put("code", -1);
            map.put("msg", "用户名或密码错误");
        }
        return map;
    }

    @RequestMapping(value = "/pc/loginOut")
    public String pcLoginOut(HttpServletRequest request) {
        request.getSession().removeAttribute("pcUser");
        return "redirect:/admin/pc/toLogin";
    }

    @RequestMapping("/pc/update")
    @ResponseBody
    public void pcUpdate(Admin admin) {
        if(admin.getId()==null){
            adminService.add(admin);
        }else{
            adminService.pcUpdate(admin);
        }

    }

    @RequestMapping("/pc/userList")
    public String userList(Model model, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("pcUser");
        if (admin != null) {
            model.addAttribute("userList", userService.getAllUser());
        } else {
            return "redirect:/admin/pc/toLogin";
        }
        return "/pcPages/userList";
    }

    @RequestMapping("/pc/index")
    public String index(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("pcUser");
        if (admin == null) {
            return "redirect:/admin/pc/toLogin";
        }
        return "/pcPages/index";
    }

    @ResponseBody
    @RequestMapping("/pc/userInfo")
    public User pcUserInfo(Model model, Integer id) {
        return userService.getUserById(id);
    }

    @RequestMapping("/pc/userUpdate")
    @ResponseBody
    public void userUpdate(User user) {
        userService.update(user);
    }


    @RequestMapping(value = "/leadWine")
    public String leadWine(Integer id,Model model,HttpServletRequest request) {
        model.addAttribute("adminId",id);
        request.getSession().setAttribute("adminId",id);
        return "/leadWine";
    }

    @RequestMapping(value = "/storeOperation")
    public String storeOperation(Model model, Integer type) {
        model.addAttribute("type", type);
        if (type == 1) {
            model.addAttribute("title", "领取会员酒");
        } else {
            model.addAttribute("title", "领取推广酒");
        }
        return "/storeOperation";
    }

    @RequestMapping(value = "/makesureWine")
    public String makesureWine(HttpServletRequest request, String mobile, Integer type, Integer num, Model model) {
        Integer id = (Integer) request.getSession().getAttribute("adminId");
        Admin admin=adminService.getAdmin(id);
//        Admin admin = adminService.getAdmin(6);
        VipCountResp vipCountResp = adminService.getReWineCount(admin, mobile, type, num);
        if(vipCountResp==null){

        }
        model.addAttribute("vipResp", vipCountResp);
        return "/makesureWine";
    }

    @RequestMapping("/receiveWine")
    @ResponseBody
    public Map receive(Integer uid, Integer num, Integer type,HttpServletRequest request) {
        Integer id = (Integer) request.getSession().getAttribute("adminId");
        Admin admin=adminService.getAdmin(id);
//        Admin admin = adminService.getAdmin(9);
        return userService.pickUpWine(uid, num, admin, type);
    }

    @RequestMapping("/getUserByMobile")
    @ResponseBody
    public Map getUserByMobile(String mobile,HttpServletRequest request) {
        Integer id = (Integer) request.getSession().getAttribute("adminId");
        return userService.getUserByMobile(mobile,id);
    }

    @RequestMapping("/pc/recordList")
    public String recordList(Model model) {
        model.addAttribute("recordList",adminService.getUserRecord());
        return "/pcPages/recordList";
    }

    @RequestMapping("/mallCode")
    public String getRedWine(Model model,HttpServletRequest request){
        Integer id = (Integer) request.getSession().getAttribute("adminUserId");
        Admin admin=adminService.getAdmin(id);
//        Admin admin=adminService.getAdmin(8);
        String codeUrl=adminService.codeUrl(admin)+"/admin/leadWine?id="+admin.getId();
        model.addAttribute("codeUrl",codeUrl);
        return "/extensionMall";
    }

    @RequestMapping("/pc/recode")
    public String recode(Model model,HttpServletRequest request){
        List<Redcode> list=adminService.getRecodeList();
        model.addAttribute("list",list);
        return "/pcPages/codeList";
    }

}
